package com.android.task4;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class OpenMapService extends Service implements LocationListener {

	private LocationManager locationManager;
	
	int xCoordinate = -100;
	int yCoordinate = -100;
	int zoom = 17;
	LocationListener locationListener;
	boolean locationUpdatesActivated = false;
	private callBackService activity = null;
	
	public void onCreate(){
		super.onCreate();
		Log.i("i", "1.OnCreate");
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
		locationListener = this;
		
	}
	
	public void onDestroy(){
		locationManager.removeUpdates(this);
		super.onDestroy();
	}
	
	public final service.Stub iBinder = new service.Stub(){
		@Override
		public int getXCoordinates() throws RemoteException {
			return xCoordinate;
		}
		@Override
		public int getYCoordinates() throws RemoteException {
			return yCoordinate;
		}
		@Override
		public void setCallback(IBinder iBinder) throws RemoteException {
			activity = callBackService.Stub.asInterface(iBinder);
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
			locationUpdatesActivated = true;
		}
		@Override
		public boolean changeMode() throws RemoteException {
		
			if (locationUpdatesActivated){
				Log.i("INFO","Changed to scroll mode ");
				locationManager.removeUpdates(locationListener);
				locationUpdatesActivated = false;
				return false;
			}
			else{
				locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
				locationUpdatesActivated = true;
				Log.i("INFO","Changed to follow-me mode ");
				return true;
			}
			
		}
		@Override
		public void decreaseZoomLevel() throws RemoteException {
			setZoom(zoom-1);
		}
		@Override
		public void increaseZoomLevel() throws RemoteException {
			setZoom(zoom+1);
		}
		@Override
		public void updateCoordinates(int x, int y) throws RemoteException {
			// TODO Auto-generated method stub
			
		}
	};
	
	public void setZoom(int zoom){
		if(zoom > 18 || zoom < 0){
			// Do nothing
		}
		else{
			this.zoom = zoom;
		}
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		return iBinder;
	}

	@Override
	public void onLocationChanged(Location loc) {
		double latitude =  loc.getLatitude();
		double longitude = loc.getLongitude();
		int xTmp = (int)Math.floor( (longitude+ 180) / 360 * (1<<zoom) ) ;
		double xOffSet = (longitude+ 180) / 360 * (1<<zoom);
		int yTmp = (int)Math.floor( (1 -Math.log(Math.tan(Math.toRadians(latitude)) + 1 / Math.cos(Math.toRadians(latitude))) / Math.PI) / 2 * (1<<zoom) ) ;
		double yOffSet = (1 -Math.log(Math.tan(Math.toRadians(latitude)) + 1 / Math.cos(Math.toRadians(latitude))) / Math.PI) / 2 * (1<<zoom);
		
		if(xTmp == xCoordinate && yTmp == yCoordinate){
			Log.i("LOC","Unchanged. xTmp = "+xTmp+" yTmp = " +yTmp);
			// Nothing has changed, do nothing.
		}
		else{
			xCoordinate = xTmp;
			yCoordinate = yTmp;
			Log.i("LOC","Changed. xCoordinate: "+xCoordinate +" yCoordinate: "+ yCoordinate);
			if(activity!=null){
				try {
					activity.updateLocation(xCoordinate, yCoordinate,xOffSet,yOffSet, zoom);
				} 
				catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void onProviderDisabled(String arg0) {
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		
	}

}

